package com.zqh.app.utils;


import java.util.*;


import com.zqh.app.utils.jiguang.JPushClient;
import com.zqh.app.utils.jiguang.push.PushResult;
import com.zqh.app.utils.jiguang.push.model.Options;
import com.zqh.app.utils.jiguang.push.model.Platform;
import com.zqh.app.utils.jiguang.push.model.PushPayload;
import com.zqh.app.utils.jiguang.push.model.audience.Audience;
import com.zqh.app.utils.jiguang.push.model.notification.AndroidNotification;
import com.zqh.app.utils.jiguang.push.model.notification.IosNotification;
import com.zqh.app.utils.jiguang.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;


public class PushUtils {
    protected static final Logger LOG = LoggerFactory.getLogger(PushUtils.class);

    protected static final String APP_KEY ="b4379485055e3f8e4e7b3145";
    protected static final String MASTER_SECRET = "33662e847e56360ccf336947";

    /**
     * 发送推送依赖设备号
     * @param platform
     * @param extras
     * @param audience
     * @param alert
     */
    public static PushResult SendPushByregistrationId(Platform platform, Map<String,String> extras, Audience audience, String alert) {
        ClientConfig clientConfig = ClientConfig.getInstance();
        final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
        final PushPayload payload = buildPushObject_android_and_ios(platform,extras, audience, alert);
        PushResult result = null;
        try {
            result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
            System.out.println(result);
            return result;
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
            LOG.error("Sendno: " + payload.getSendno());

        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
            LOG.error("Sendno: " + payload.getSendno());
        }
        return result;
    }
    /**
     * 使用registrationId推送
     * @return
     */
    private static PushPayload buildPushObject_android_and_ios(Platform platform,Map<String,String> extras, Audience audience,String alert) {
        return PushPayload.newBuilder()
                .setPlatform(platform)
                .setAudience(audience)
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtras(extras).build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtras(extras).build())
                        .build())
                .build();
    }
//    public static void main(String[] arge){
//        Map extras = new HashMap(); //extras（额外参数）
//        extras.put("fType","health");
//        extras.put("sType","");
//        extras.put("sungoId","");//extras（额外参数）
//        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
//        //stringCollection.add("191e35f7e013dc92402");
//        stringCollection.add("13065ffa4e599bcf740");
//        String alert = "松果健康为您服务..........，请开始您的健康之旅";  //通知内容
//        //String title = "通知标题";   //通知标题，默认不给
//        PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), alert);
//    }
}

